package com.mql.gre.rs.client;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamReader;

import com.mql.gre.beans.Document;
import com.mql.gre.beans.DocumentsList;
import com.mql.gre.beans.Produit;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class MainClient {
	public MainClient() throws Throwable {
		// produitClient();
		// gdeRESTClient(); // Client pour gdeREST
		jerseyClient();
		// jerseyListClient();

	}

	private void jerseyListClient() throws JAXBException {

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(UriBuilder.fromUri(
				"http://localhost:8080/gdeREST").build());
		String s = service.path("documents").path("prod/all")
				.accept(MediaType.APPLICATION_XML).get(String.class);
		System.out.println(s);

	}

	private void jerseyClient() throws JAXBException {

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(UriBuilder.fromUri(
				"http://localhost:8080/gdeREST").build());
		// String s = service.path("documents").path("prod")
		// .accept(MediaType.APPLICATION_XML).get(String.class);
		InputStream inp = service.path("documents").path("all")
				.accept(MediaType.APPLICATION_XML).get(InputStream.class);
		// System.out.println(s);

		// JAXBContext jxb = JAXBContext.newInstance(Document.class);

		JAXBContext jxb = JAXBContext.newInstance(DocumentsList.class);

		DocumentsList dl = (DocumentsList) jxb.createUnmarshaller().unmarshal(
				inp);

		// Document d = (Document) jxb.createUnmarshaller().unmarshal(inp);

		System.out.println(dl.getList());

	}

	private void gdeRESTClient() throws Exception {

		URL url = new URL("http://localhost:8080/gdeREST/documents/xml/001");
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("GET");
		connection.setRequestProperty("Accept", "application/xml");
		
		if (connection.getResponseCode() != 200) {
			throw new RuntimeException("Operation failed: "
					+ connection.getResponseCode());
		}
		System.out.println("Content-Type: " + connection.getContentType());
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
		String line = reader.readLine();
		while (line != null) {
			System.out.println(line);
			line = reader.readLine();
		}
		connection.disconnect();
	}

	private void produitClient() throws Exception {

		URL url = new URL("http://localhost:8080/gdeREST/documents/prod");
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("GET");
		connection.setRequestProperty("Accept", "application/xml");
		if (connection.getResponseCode() != 200) {
			throw new RuntimeException("Operation failed: "
					+ connection.getResponseCode());
		}
		System.out.println("Content-Type: " + connection.getContentType());

		// BufferedReader reader = new BufferedReader(new InputStreamReader(
		// connection.getInputStream()));
		// String line = reader.readLine();
		// while (line != null) {
		// System.out.println(line);
		// line = reader.readLine();
		// }

		JAXBContext jxb = JAXBContext.newInstance(Produit.class);

		Produit p = (Produit) jxb.createUnmarshaller().unmarshal(
				connection.getInputStream());

		System.out.println(p);

		connection.disconnect();

	}

	public static void main(String[] args) throws Throwable {
		new MainClient();

	}

}
